﻿@using Think9.Models;
@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<input type="text" name="Password" id="Password" style="display:none" value="@ViewBag.Password">
<fieldset class="table-fieldset" id="searchfield" style="display:none">
    <legend style="color:darkgrey">快速查询</legend>
    <div style="margin: 5px 5px 5px 5px">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <select id="DeptNo" name="DeptNo">
                        <option value="">==请选择==</option>
                        @foreach (valueTextEntity item in ((IEnumerable<valueTextEntity>)ViewBag.SelectList))
                        {
            <option value="@item.Value">@item.Text</option>}
                    </select>
                </div>
                <div class="layui-inline">
                    @Html.DropDownList("RoleId", (IEnumerable<SelectListItem>)ViewBag.RoleList, "==请选择==", new Dictionary<string, object> { })
                </div>
                <div class="layui-inline">
                    <select id="EnabledMark" name="EnabledMark">
                        <option value="">==请选择==</option>
                        <option value="0">启用</option>
                        <option value="1">禁用</option>
                    </select>
                </div>
                <div class="layui-inline">
                    <input class="layui-input" placeholder="关键字" name="key" id="key" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn layui-btn-normal" id="search"><i class="layui-icon layui-icon-search"></i></button> <button type='reset' class='layui-btn layui-btn-primary'><i class='layui-icon layui-icon-refresh'></i></button>
                </div>
            </div>
        </form>
    </div>
</fieldset>
<!--数据表格-->
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "miniPage"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        var openWH = miniPage.getOpenWidthHeight();
        let $ = layui.$;

        form.render();

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysBasic/User/GetPageListBySearch?key=" + $("#key").val() + "&dep=" + $("#DeptNo").val() + "&enabled=" + $("#EnabledMark").val() + "&roleId=" + $("#RoleId").val(),
            limits: [10, 50, 100],
            limit: 10,
            method: "POST",
            page: true,
            defaultToolbar: [{ title: '搜索', layEvent: 'searchShow', icon: 'layui-bg-blue layui-icon-search' }, 'filter', 'exports'],
            toolbar: "#toolbarTpl",
            cols: [[
                { type: "checkbox", fixed: 'left' },
                { type: "numbers", title: "NO.", fixed: 'left' },
                { field: "Account", title: "账户", sort: true, width: 150 },
                { field: "RealName", title: "姓名", sort: true, width: 200 },
                { field: "DepartmentName", title: "部门", width: 200, sort: true },
                { field: "RoleName", title: "角色", width: 150, sort: true },
                { field: "EnabledMark", title: "启用/禁用", width: 80, align: "center", templet: "#enabledMarkTpl", sort: true },
                { title: "操作", width: 200, align: "center", fixed: layui.device().mobile ? null : 'right', templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                if (layui.device().mobile) {
                    $("#add").html("<i class='fa fa-plus'></i>新建");
                    $("#batchDel").html('<i class="fa fa-trash-o"></i>删除');
                }
            }
        });

        form.on("submit(search)", function (data) {
            ThisTable.reload({
                where: data.field,
                page: { curr: 1 }
            });
            return false;
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "batchDel":
                    batchDel();
                    break;
                case "searchShow":
                    searchShow();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.Id);
                    break;
                case "del":
                    del(data.Id);
                    break;
                case "pwdReset":
                    pwdReset(data.Id);
                    break;
            }
        });

        function searchShow() {
            var display = $('#searchfield').css('display');
            if (display == 'none') {
                $("#searchfield").show();
                document.body.scrollTop = document.documentElement.scrollTop = 0;
            }
            else {
                $("#searchfield").hide();
            }
        }

        //查询
        $("#search").click(function () {
            search();
        });

        function add() {
            exLayer.openMiddle("新建用户", "/SysBasic/User/Add", '500px', '450px', layui.device().mobile);
        }

        function edit(id) {
            exLayer.openMiddle("编辑用户", "/SysBasic/User/Edit/" + id, '500px', '450px', layui.device().mobile);
        }

        function batchDel() {
            exLayer.confirm("确定要删除吗？", function (index) {
                layer.close(index);
                let idsStr = exUtils.tableBatchCheck(table);
                if (idsStr) {
                    exUtils.ajax("/SysBasic/User/BatchDel", "get", { idsStr: idsStr }, true).done(function (response) {
                        exUtils.tableSuccessMsg(response.message);
                        $(".layui-laypage-btn")[0].click();
                    }).fail(function (error) {
                        console.log(error);
                    });
                }
            });
        }

        function del(id) {
            exLayer.confirm("确定要删除吗？", function () {
                exUtils.ajax("/SysBasic/User/Delete", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    $(".layui-laypage-btn")[0].click();
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function pwdReset(id) {
            exLayer.confirm("用户密码将重置为" + $("#Password").val() + "，确定要重置密码吗？", function () {
                exUtils.ajax("/SysBasic/User/InitPwd", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function search() {
            $("#searchfield").hide();
            ThisTable.reload({
                where: { key: $("#key").val(), dep: $("#DeptNo").val(), enabled: $("#EnabledMark").val(), roleId: $("#RoleId").val() },
                page: { curr: 1 }
            });
            return false;
        }
    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <button class='layui-btn layui-btn-sm layui-btn-danger' lay-event='batchDel' id='batchDel'><i class="fa fa-remove"></i>批量删除</button>
    <button type="button" class='layui-btn layui-btn-sm layui-btn-normal' lay-event='add' id='add'><i class="fa fa-plus"></i>新建用户</button>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a class='layui-btn layui-btn-normal layui-btn-xs ' href='javascript:;' lay-event='edit' id='edit'>编辑</a>
    <a class='layui-btn layui-btn-xs layui-btn-danger' href='javascript:;' lay-event='del' id='del'>删除</a>
    <a class='layui-btn layui-btn-xs layui-btn-warm' href='javascript:;' lay-event='pwdReset' id='pwdReset'>重置密码</a>
</script>
<!-- 启用|停用模板 -->
<script type="text/html" id="enabledMarkTpl">
    {{#  if(!d.EnabledMark){ }}
    <span class="layui-btn layui-btn-normal layui-btn-xs">启用</span>
    {{#  } else{ }}
    <span class="layui-btn layui-btn-warm layui-btn-xs">禁用</span>
    {{#  } }}
</script>
<!-- 性别模板 -->
<script type="text/html" id="genderTpl">
    {{#  if(d.Gender){ }}
    <span>男</span>
    {{#  } else{ }}
    <span>女</span>
    {{#  } }}
</script>